package ar.com.solidussource.howmuchmobile.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import ar.com.solidussource.howmuchmobile.R;
import ar.com.solidussource.howmuchmobile.activities.generic.BaseActivity;
import ar.com.solidussource.howmuchmobile.activities.list.AddressListActivity;
import ar.com.solidussource.howmuchmobile.services.PlacesGoogleService;

import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: FT31270775
 * Date: 26/06/13
 * Time: 15:39
 * To change this template use File | Settings | File Templates.
 */
public class AddressSearchGeocoderActivity extends BaseActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = (View) inflater.inflate(R.layout.address_search, null);

        AutoCompleteTextView autoCompView = (AutoCompleteTextView) v.findViewById(R.id.myautocomplete);
        autoCompView.setAdapter(new PlacesAutoCompleteAdapter(this, android.R.layout.simple_dropdown_item_1line));

        setContent(v);
    }


    private class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
        private ArrayList<String> resultList;

        public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        @Override
        public int getCount() {
            return resultList.size();
        }

        @Override
        public String getItem(int index) {
            return resultList.get(index);
        }

        @Override
        public Filter getFilter() {
            Filter filter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint != null) {
                        // Retrieve the autocomplete results.
                        PlacesGoogleService placesGoogleService = new PlacesGoogleService();
                        resultList = placesGoogleService.autocomplete(constraint.toString());

                        // Assign the data to the FilterResults
                        filterResults.values = resultList;
                        filterResults.count = resultList.size();
                    }
                    return filterResults;
                }

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    }
                    else {
                        notifyDataSetInvalidated();
                    }
                }};
            return filter;
        }
    }

}